package com.ti.shangou.controller.pages.back.goods;

import com.ti.shangou.controller.BaseController;
import com.ti.shangou.dao.GoodsDao;
import com.ti.shangou.dao.MerchantDao;
import com.ti.shangou.pojo.dto.PageDTO;
import com.ti.shangou.pojo.dto.ResponseDTO;
import com.ti.shangou.pojo.entity.Goods;
import com.ti.shangou.pojo.query.GoodsQuery;
import com.ti.shangou.pojo.vo.GoodsTypeVO;
import com.ti.shangou.pojo.vo.MerchantVO;
import com.ti.shangou.pojo.vo.UserVO;
import com.ti.shangou.service.impl.GoodsServiceImpl;
import com.ti.shangou.service.impl.GoodsTypeServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/pages/back/goods")
public class GoodsController extends BaseController {
    @Resource
    GoodsTypeServiceImpl goodsTypeService;
    @Resource
    MerchantDao merchantDao;
    @Resource
    GoodsServiceImpl goodsService;
    @Resource
    GoodsDao goodsDao;

    @RequestMapping("list")
    String list() {
        return "pages/back/goods/goods-list";
    }

    @RequestMapping("addPre")
    public String toAddPre(Model model){
        List<GoodsTypeVO> goodsTypeVOS = goodsTypeService.selectGoodsType();
        model.addAttribute("goodsTypes",goodsTypeVOS);
        return "pages/back/goods/goods-addPre";
    }

    @RequestMapping("/merchantList")
    public String goodsList(Model model){
        List<GoodsTypeVO> goodsTypeVOS = goodsTypeService.selectGooodsTypeByMerchantId(getMerchantId());
        model.addAttribute("goodsTypes",goodsTypeVOS);
        return "/pages/back/goods/goods-merchantList";
    }

    @ResponseBody
    @RequestMapping("add")
    public Object addGoods(Goods goods){
        Date date = new Date();
        goods.setCreateTime(date);
        goods.setUpdateTime(date);
        goods.setUpdateUser(getUserId());
        goods.setMerchantId(getMerchantId());

        ResponseDTO responseDTO = goodsService.addGoods(goods);
        return responseDTO;
    }

    @ResponseBody
    @RequestMapping("/ajaxList")
    public Object ajaxList(GoodsQuery goodsQuery){
        if (goodsQuery.getIsAll()==null){ // isAll为空，查询某个商户
            UserVO user = (UserVO) getSession().getAttribute("user");
            MerchantVO merchantVO = merchantDao.selectMerchantByUserId(user.getUserId());
            goodsQuery.setMerchantId(merchantVO.getMerchantId());
        }
        PageDTO pageDTO = goodsService.ajaxGoodsList(goodsQuery);
        return pageDTO;
    }


    @ResponseBody
    @RequestMapping("delete/{goodsId}")
    public Object deleteGoods(@PathVariable Long goodsId){
        return ResponseDTO.get(goodsDao.deleteByPrimaryKey(goodsId)==1);
    }

    @ResponseBody
    @RequestMapping("edit")
    public Object editGoods(Goods goods){
        return ResponseDTO.get(goodsDao.updateByPrimaryKeySelective(goods)==1);
    }

}
